/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChartCanvas.java
 *
 * Created on 04-nov-2011, 18:57:13
 */
package org.itver.arm.views.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;

/**
 *
 * @author pablo
 */
public class ChartCanvas extends javax.swing.JPanel{
    private static ChartCanvas instance;
    private final Color COLOR_BG = Color.WHITE;
    private final Color COLOR_GRID = Color.GRAY;
    private final Color COLOR_AXES = Color.BLACK;
    private final int MARGIN = 5;
    private final int AXIS_MARGIN = MARGIN * 2;
    private final int NUMBER_COLS = 6;
    private final int NUMBER_ROWS = 10;
    
    private ArrayList<ChartData> datas;
    private int width;
    private int height;
    private double maxValue;
    
    /** Creates new form ChartCanvas */
    private ChartCanvas() {
        initComponents();
        this.datas = new ArrayList<ChartData>();
    }
    
    public synchronized static ChartCanvas singleton(){
        if(instance == null)
            instance = new ChartCanvas();
        return instance;
    }
    
    @Override
    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        width = this.getWidth();
        height = this.getHeight();
        g2d.setBackground(COLOR_BG);
        g2d.clearRect(0, 0, width, height);
        paintChart(g2d);
    }
    
    public void addData(ChartData data){
        this.datas.add(data);        
    }
    
    public void reset(){
        datas.clear();
    }
    
    private void paintChart(Graphics2D g2){
        paintGrid(g2);
        for(ChartData data : this.datas)
            paintData(g2, data);
        paintAxes(g2);
    }
    
    private void paintGrid(Graphics2D g2){
        maxValue = 0;
        for(ChartData data: this.datas)
            if(data.getMax() > maxValue)
                maxValue = data.getMax();
        paintXGrids(g2, (int)maxValue);
        paintYGrids(g2, this.datas.size() > 0 ? (int)this.datas.get(0).getTime() : 0);
    }
    
    private void paintXGrids(Graphics2D g2, int max){
        int space = Math.round((this.height - AXIS_MARGIN * 2) / NUMBER_COLS);
        double jump = max / NUMBER_COLS;
        Stroke stroke = new BasicStroke(0.5f);
        g2.setStroke(stroke);
        g2.setColor(COLOR_GRID);
        g2.setFont(new Font("Arial", Font.PLAIN, 8));
        for(int i = 1; i <= NUMBER_COLS; i++){
            int y = height - i * space + AXIS_MARGIN;
            g2.drawString(String.valueOf(jump * i), MARGIN, y);
            g2.drawLine(AXIS_MARGIN, y, this.width - AXIS_MARGIN, y);
        }
    }
    
    private void paintYGrids(Graphics2D g2, int max){
        int space = Math.round((this.width - AXIS_MARGIN * 2) / NUMBER_ROWS);
        int jump = Math.round(max / NUMBER_ROWS);
        for(int i = 0; i <= NUMBER_ROWS; i++){
            int x = i *  space + AXIS_MARGIN;
            g2.drawString(String.valueOf(jump * i + "s"), x, this.height - MARGIN);
            g2.drawLine(x, this.height - AXIS_MARGIN * 2, x, AXIS_MARGIN);
        }
    }
    
    private void paintAxes(Graphics2D g2) {
        g2.setColor(COLOR_AXES);
        Stroke stroke = new BasicStroke(1.5f);
        g2.setStroke(stroke);
        g2.drawLine(AXIS_MARGIN, AXIS_MARGIN, AXIS_MARGIN, this.height - AXIS_MARGIN);
        g2.drawLine(AXIS_MARGIN, this.height - AXIS_MARGIN, this.width - AXIS_MARGIN, this.height - AXIS_MARGIN);
    }
    
    private void paintData(Graphics2D g2, ChartData data){
        Color color = data.getColor();
        Stroke stroke = new BasicStroke(2.5f);
        g2.setColor(color);
        g2.setStroke(stroke);
        AffineTransform transform = new AffineTransform();
        transform.translate(AXIS_MARGIN, AXIS_MARGIN);
        Shape dataShape = transform.createTransformedShape(
                        data.getShape(width - AXIS_MARGIN * 2, height - AXIS_MARGIN * 2, maxValue));
        g2.clipRect(AXIS_MARGIN, AXIS_MARGIN, width - AXIS_MARGIN * 2 - 1, height - AXIS_MARGIN * 2 - 1);
        g2.draw(dataShape);
        g2.setClip(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
